package es.ats.codington.web.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import es.ats.codington.festival.services.EventSignUpService;
import es.ats.codington.festival.services.VisitorService;

/**
 * Servlet implementation class EventSignupServlet
 */
@WebServlet("/EventSignupServlet")
public class EventSignupServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EventSignupServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int eventId =0;
		String userName = null;
		Cookie cookie= null;
		Cookie[] cookies=request.getCookies();
		if(cookies!=null){
			for(int i=0; i<cookies.length; i++){
				cookie=cookies[i];
				if(cookie.getName().compareTo("user")==0)
				userName=cookie.getValue();
				
			}
		}else{
			response.sendRedirect("login.jsp");
		}
		eventId= Integer.parseInt(request.getParameter("eventId"));
		EventSignUpService eventSignUpService = new EventSignUpService();
		VisitorService visitorService = new VisitorService();
		//response.sendRedirect("events.jsp");
		eventSignUpService.signUpToEvent(visitorService.getVisitorIdService(userName), eventId);
		RequestDispatcher rd= request.getRequestDispatcher("index2.jsp");
		rd.forward(request, response);
	}

}
